/*
	js公共方法
	
 */

//定义一个全局变量
var C = {} ;

//将Param中的参数转成Json格式对象
C.getParams = function(param){
	if(param == null || param == ""){
		return null ;
	}
	var _data = {} ;
	var _p = param.substr(1,param.length-2);
	var _o = _p.split(", ");
	for(index in _o){
		var _v = _o[index];
		if(_v.split("=")[1]){
			_data[_v.split("=")[0]] = _v.split("=")[1];
		}
	}
	return _data ;
}

//table数据加载
C.load=function($t,param){
	var _url = $t.attr("url");
	var _ths = [];      //标题行
	var _primaryKey = [] ;  //唯一索引字段
	var _page = null ;  //分页内容
	var _text = {} ;    //返回结果
	$t.find("thead tr th").each(function(){
		var $this = $(this) ;
		_ths.push($this.attr("name"));
		if($this.attr("primaryKey")=="true"){
			_primaryKey.push($this.attr("name")) ;
		}
	});
	$.ajax({
		url:_url,
		data:param,
		type:"post",
		success:function(text){
			if(text.success){
				_page = text.data;
				_text = text ;
				var data = text.data.list ;
				var tbody = "";
				for(var i=0;i<data.length;i++){
					var tr = '<tr class="gradeA ">';
					for(var index in _ths){
										if(_ths[index]!="work-box"){
											var td_text = ((data[i][_ths[index]]==null)?"":data[i][_ths[index]]);
											tr+='<td class=" " >'+ (td_text.toString().length<=10?td_text:(td_text.toString().substring(0,9)+'...'))   +'</td>';
										}else if(_ths[index]=="work-box"){
											tr+='<td class="center ">';
											tr+='<a href="#" onclick="edit($(this))"><span class="glyphicon glyphicon-pencil"></span></a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="del($(this))"><span><span class="glyphicon glyphicon-remove"></span></span></a>';
											tr+='<form action="" method="post">';
											for(var key in _primaryKey){
												tr+='<input type="hidden" name="'+ _primaryKey[key] +'" value="'+ data[i][_primaryKey[key]] +'"/>';
											}
											tr+='</form>';
											tr+='</td>';
										}
									}
							 '</tr>' ;
					 tbody+=tr;	
				}
				$t.find("tbody").html(tbody);
				//分页面内容
				$("#_paging").empty();
				$("#_paging").html('<a class="first ui-button '+ (_page==null||_page.isFirstPage?'ui-state-disabled':'') +'" onclick="toPage(1,'+ _page.pageSize +')">首页</a>'+
											    '<!-- ui-state-disabled -->'+
												'<a class="previous ui-button '+ (_page==null||!_page.hasPreviousPage?'ui-state-disabled':'') +'" onclick="toPage('+ (_page==null?'':_page.prePage) +','+ _page.pageSize +')">上一页</a>'+
												'<span class="ui-button ui-state-default ">'+ (_page==null?0:_page.pageNum) +'/'+ (_page==null?0:_page.pages)+'页   共'+ (_page==null?0:_page.total) +'条' +'</span>'+
												'<a class="next  ui-button '+ (_page==null||!_page.hasNextPage?'ui-state-disabled':'') +'" onclick="toPage('+ (_page==null?'':_page.nextPage) +','+ _page.pageSize +')">下一页</a>'+
												'<a class="last ui-button '+ (_page==null||_page.isLastPage?'ui-state-disabled':'') +'" onclick="toPage('+ (_page==null?'':_page.pages) +','+ _page.pageSize +')">末页</a>');
			}else{
				_page = text.data;
				console.log(_page);
				//分页面内容
				$("#_paging").empty();
				$("#_paging").html('<a class="first ui-button '+ (_page==null||_page.isFirstPage?'ui-state-disabled':'') +'" onclick="toPage(1,'+ _page.pageSize +')">首页</a>'+
											    '<!-- ui-state-disabled -->'+
												'<a class="previous ui-button '+ (_page==null||!_page.hasPreviousPage?'ui-state-disabled':'') +'" onclick="toPage('+ (_page==null?'':_page.prePage) +','+ _page.pageSize +')">上一页</a>'+
												'<span class="ui-button ui-state-default ">'+ (_page==null?0:_page.pageNum) +'/'+ (_page==null?0:_page.pages)+'页   共'+ (_page==null?0:_page.total) +'条' +'</span>'+
												'<a class="next  ui-button '+ (_page==null||!_page.hasNextPage?'ui-state-disabled':'') +'" onclick="toPage('+ (_page==null?'':_page.nextPage) +','+ _page.pageSize +')">下一页</a>'+
												'<a class="last ui-button '+ (_page==null||_page.isLastPage||true?'ui-state-disabled':'') +'" onclick="toPage('+ (_page==null?'':_page.pages) +','+ _page.pageSize +')">末页</a>');				
			}
			return text.success ;
		}
	});
}



//表单非空验证（索引查询时调用）
C.toVail = function(_this){
	var flag = true ;
	_this.find("input").each(function(){
		_input = $(this) ;
		if(_input.val()=="" || _input.val()==null){
			_input.next(".error_msg").html("<font color='red'>不能为空！</font>");
			_input.next(".error_msg").css("display","block");
			flag = false ;
		}else{
			_input.next(".error_msg").html("");
			_input.next(".error_msg").css("display","none");
		}
	});
	if(flag){
		return true;
	}else{
		return false ;
	}
}

//main页面切换iframe
C.toUrl = function(_this,iframeId){
	_url = _this.attr("url");
	$("#"+iframeId).attr("src",_url);
	//location.href=_url;
}

/*
 * 弹出对话框message_box,显示信息
 * 
 * @param opts
 * 			1. {String} 提示信息 
 * 			2. {JSON}
 * 				msg			: 提示信息内容
 * 				timeout		: 显示时间
 * 				position	: 显示位置
 * 				type		: 提示类别
 */
C.showMsg = function(opts){
	if (!$.isPlainObject(opts)) {
		opts = {
			msg : opts || '操作成功！'
		};
	}
	opts = $.extend({
		position : {
			my:"right top",
			at:"right top+30",
			of:"body"	
		},
		timeout : 3000,
		type : 'success'
	}, opts);
	var $box = $('<div class="message-box">');
	$box.append($('<i class="icon-success">')).append(opts.msg);
	$('body',window.parent.document).append($box);
	
	$box.position(opts.position);//依赖jQuery-ui
	$box.fadeIn(200, function() {
		$box.fadeOut(opts.timeout, function() {
			$(this).remove();
		});
	});
}

C.gritter = function(msg){
	$.gritter.add({
		// (string | mandatory) the heading of the notification
		title: '操作结果：',
		// (string | mandatory) the text inside the notification
		text: msg			
		});

	return false;
}

/***
 * 显示验证提示信息
 */
C.handleError = function($f,errors){
	for(field in errors){
		$this = $f.find("span[name='error_"+field+"']");
		$this.html("<font color='red'>"+errors[field]+"</font>");
		$this.css("display","inline");
	}
};
